<!doctype html>
<html lang="html">
<head>
    <title>ScriptX WASM UnitTests</title>
    <style>
        body {
            font-family: 'Source Code Pro', sans-serif;
            padding: 2em;
        }

        ul {
            list-style-type: none;
        }

        li {
            border-bottom-color: #ececec;
            border-bottom-width: 1px;
            border-bottom-style: solid;
            min-height: 1em;
            white-space: pre;
        }

        li:hover {
            background-color: #039be51a;
        }

        .log {
        }

        .info {
        }

        .debug {
        }

        .warn {
            color: orangered;
        }

        .error {
            color: red;
        }

        @media (prefers-color-scheme: dark) {
            body {
                color: #eee;
                background: #121212;
            }

            li {
                border-bottom-color: #222222;
            }

            input {
                color: #eee;
            }
        }
    </style>
    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>

<div class="row"></div>
<form id="filter_form">
    <div class="input-field">
        <i class="material-icons prefix">settings_input_component</i>
        <label for="test_filter">Command Line Arguments</label>
        <input type="text" id="test_filter" class="materialize-textarea" autofocus/>
    </div>
</form>

<!--html configs-->
<ul id="consoleLogContainer"></ul>

<div id="refreshFAB" class="fixed-action-btn">
    <a class="btn-floating btn-large waves-effect waves-light red">
        <i class="large material-icons" id="fab_icon">arrow_downward</i>
    </a>
</div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

<script type="application/javascript"
        src="https://cdn.rawgit.com/Alorel/console-log-html/master/console-log-html.min.js"></script>

<script>
    const consoleLogContainer = document.getElementById("consoleLogContainer")
    // https://alorel.github.io/console-log-html/jsdoc/index.html
    ConsoleLogHTML.connect(
        consoleLogContainer,
        {
            "log": "log",
            "info": "info",
            "debug": "debug",
            "warn": "warn",
            "error": "error",
        },
        false,
        true,
        true
    );

    const oldLog = console.log;
    console.log = function () {
        // failed UnitTest
        if (typeof arguments[0] === 'string' &&
            arguments[0].match(/\[\s*FAILED\s*]/)
        ) {
            // console.error.apply(this, arguments);
            console.error(arguments[0])
        } else {
            oldLog.apply(this, arguments);
        }
    }

    document.addEventListener('DOMContentLoaded', function () {
        M.updateTextFields();
        var elems = document.querySelectorAll('.fixed-action-btn');
        var options = {};
        var instances = M.FloatingActionButton.init(elems, options);
    });

    const refreshFAB = document.getElementById("refreshFAB");
    refreshFAB.onclick = function () {
        window.scrollTo(0, document.body.scrollHeight);
    }

    test_filter.value = localStorage.getItem("test_filter");
    filter_form.addEventListener("submit", (event) => {
        localStorage.setItem("test_filter", test_filter.value);
        event.preventDefault();
        window.location.reload();
    });
</script>

<!--run wasm-->

<script>

    function performRunUnitTest() {
        consoleLogContainer.innerHTML = "";
        const args = test_filter.value.split(' ');
        console.log("run main with args: " + JSON.stringify(args));
        try {
            Module.callMain(args);
        } catch (e) {
            console.error("failed to run UnitTests", e);
        }
    }

    var Module = {
        // Don't run main() on page load
        noInitialRun: true,

        onRuntimeInitialized: function () {
            performRunUnitTest();
        }
    };
</script>
<script src="UnitTests.js"></script>
</body>
</html>
